package com.dhh.toodledo.activity;

import greendroid.app.GDActivity;
import greendroid.widget.ActionBarItem;
import greendroid.widget.ActionBarItem.Type;

import com.dhh.toodledo.R;
import com.dhh.toodledo.exception.InValidAccountException;
import com.dhh.toodledo.util.AuthUtil;
import com.flurry.android.FlurryAgent;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;

public class TDAActivity extends GDActivity  implements View.OnClickListener{
	private AuthUtil au;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setActionBarContentView(R.layout.home);
        au = new AuthUtil(getSharedPreferences("myPre", 0));
        
        initActionBar();
        findViewById(R.id.home_all_tasks).setOnClickListener(this);
        findViewById(R.id.home_due_date).setOnClickListener(this);
        findViewById(R.id.home_priority).setOnClickListener(this);
        findViewById(R.id.home_star).setOnClickListener(this);
        findViewById(R.id.home_complete).setOnClickListener(this);
        findViewById(R.id.home_hotlist).setOnClickListener(this);
        findViewById(R.id.home_signout).setOnClickListener(this);
    }
    
    @Override
    public void onStart(){
       super.onStart();
       FlurryAgent.onStartSession(this, "2ND7X5T2HQCRXGXJ1BUF");
    }
    
    @Override
    public void onStop(){
       super.onStop();
       FlurryAgent.onEndSession(this);
    }
    
    @Override
    public void onResume(){
    	super.onResume();
    	try {
			au.getKeyValue();
		} catch (InValidAccountException e) {
			Intent intent = new Intent(TDAActivity.this,LoginActivity.class);
        	TDAActivity.this.startActivity(intent);
		}
    }

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.home_all_tasks){
			Intent intent = new Intent(TDAActivity.this,ShowAllTaksActivity.class);
			startActivity(intent);
		}else if(v.getId() == R.id.home_due_date){
			Intent intent = new Intent(TDAActivity.this,ShowDueDateActivity.class);
			startActivity(intent);
		}else if(v.getId() == R.id.home_priority){
			Intent intent = new Intent(TDAActivity.this,ShowPriorityTaskActivity.class);
			startActivity(intent);
		}else if(v.getId() == R.id.home_star){
			Intent intent = new Intent(TDAActivity.this,ShowAllTaksActivity.class);
			intent.putExtra("showType", "star");
			startActivity(intent);
		}else if(v.getId() == R.id.home_complete){
			Intent intent = new Intent(TDAActivity.this,ShowAllTaksActivity.class);
			intent.putExtra("showType", "complete");
			startActivity(intent);
		}else if(v.getId() == R.id.home_hotlist){
			Intent intent = new Intent(TDAActivity.this,ShowAllTaksActivity.class);
			intent.putExtra("showType", "hotlist");
			startActivity(intent);
		}else if(v.getId() == R.id.home_signout){
			Intent intent = new Intent(TDAActivity.this,LoginActivity.class);
			startActivity(intent);
		}
	}
	
	public void initActionBar(){
        getActionBar().removeViewAt(0);
		getActionBar().setTitle("Toodledo Assistant");
		getActionBar().setGravity(Gravity.CENTER_HORIZONTAL);
        getActionBar().setBackgroundColor(Color.rgb(9, 135, 205));
        addActionBarItem(Type.Add, R.id.action_bar_home_add);
	}
	
	@Override
    public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {
		switch (item.getItemId()) {
		case R.id.action_bar_home_add:
			Intent intent = new Intent(TDAActivity.this,AddTaskActivity.class);
			this.startActivity(intent);
            break;
		}
		
		return true;
	}
}